local a = CreateFrame("Frame")
function a.CountAmmo()
	for bags = 1,4 do
	local BagName = GetBagName(bags)		
	local subtype = select(7, GetItemInfo(BagName))
		if subtype == "Quiver" or subtype == "Ammo Pouch" then
			local numslots = GetContainerNumSlots(bags)
			for slot = 1,numslots do
			local ItemLink = GetContainerItemLink(bags, slot)
				if ItemLink then
				local ItemSub = select(6, GetItemInfo(ItemLink))
					if ItemSub == "Projectile" then
						local Current = GetItemCount(ItemLink)
						local Total = (GetContainerNumSlots(bags)*200)
						local needed = math.floor((Total - Current)/200)
						return needed, ItemLink
					end
				end
			end
		end
	end
end

function a.BuyAmmo()
local needed, item = a.CountAmmo()
	if needed > 0 then
		for MerchID = 0,GetMerchantNumItems() do
			local ItemLinkMerch = GetMerchantItemLink(MerchID)
			if ItemLinkMerch == item then
				for i = 1,needed do
				ChatFrame1:AddMessage(needed)
					BuyMerchantItem(MerchID)
				end
				break
			end
		end
	end
end

a:SetScript("OnEvent", function(self, event, ...)
	if event == "MERCHANT_SHOW" then
		a.BuyAmmo()
	end
end)

a:RegisterEvent("MERCHANT_SHOW")